# Makefile for Medical Report API

.PHONY: help install dev-install test lint format clean build docker-build docker-up docker-down deploy

# 默认目标
help:
	@echo "Medical Report API - 可用命令:"
	@echo "  install       - 安装生产依赖"
	@echo "  dev-install   - 安装开发依赖"
	@echo "  test          - 运行测试"
	@echo "  lint          - 代码检查"
	@echo "  format        - 代码格式化"
	@echo "  clean         - 清理临时文件"
	@echo "  build         - 构建应用"
	@echo "  docker-build  - 构建Docker镜像"
	@echo "  docker-up     - 启动Docker服务"
	@echo "  docker-down   - 停止Docker服务"
	@echo "  deploy        - 部署到生产环境"

# 安装依赖
install:
	pip install -e .

dev-install:
	pip install -e .[dev,test]

# 测试
test:
	pytest tests/ -v --cov=src/medical_report_api --cov-report=html --cov-report=term

test-fast:
	pytest tests/ -v -x

test-integration:
	pytest tests/test_api.py -v

# 代码质量
lint:
	flake8 src/ tests/
	mypy src/

format:
	black src/ tests/
	isort src/ tests/

# 清理
clean:
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	rm -rf .pytest_cache/
	rm -rf .mypy_cache/
	rm -rf htmlcov/
	rm -rf .coverage
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete

# 构建
build: clean
	python -m build

# Docker操作
docker-build:
	docker build -t medical-report-api:latest .

docker-up:
	docker-compose up -d

docker-down:
	docker-compose down

docker-logs:
	docker-compose logs -f

docker-restart:
	docker-compose restart

# 生产环境
deploy-prod:
	docker-compose -f docker-compose.prod.yml up -d

stop-prod:
	docker-compose -f docker-compose.prod.yml down

# 数据库操作
db-init:
	# 数据库初始化脚本
	@echo "初始化数据库..."

db-migrate:
	# 数据库迁移
	@echo "执行数据库迁移..."

db-backup:
	# 数据库备份
	@echo "备份数据库..."

db-restore:
	# 数据库恢复
	@echo "恢复数据库..."

# 开发工具
dev-server:
	uvicorn src.medical_report_api.main:app --reload --host 0.0.0.0 --port 8000

dev-docs:
	python -c "import webbrowser; webbrowser.open('http://localhost:8000/docs')"

# 性能测试
benchmark:
	# 使用locust进行性能测试
	locust -f tests/locustfile.py --host=http://localhost:8000

# 代码统计
stats:
	@echo "代码统计:"
	@find src/ -name "*.py" | xargs wc -l | tail -1
	@find tests/ -name "*.py" | xargs wc -l | tail -1
	@echo "总代码行数: $$(find src/ tests/ -name '*.py' | xargs wc -l | tail -1 | awk '{print $$1}')"

# 安全检查
security:
	bandit -r src/
	safety check

# 完整CI流程
ci: lint test security
	@echo "CI流程完成"

# 发布流程
release: clean build test
	@echo "准备发布..."
	# twine upload dist/*

.DEFAULT_GOAL := help